<?php
require('header.php');
require('inStock.php');
?>

 <script type = "text/javascript">
 	function change() {
 		document.getElementById("carForm").submit();
 	}
 </script>

<?php


function carForm($con) {
	if(!isset($_POST["make"]) or ($_POST["make"] == "default")) {
		$make = "----Select Make----";
	}
	else
		$make = $_POST["make"];				
	echo 'Make: <select name="make" onchange="change()">
		<option value="' . $make . '">' . $make . '</option>';
	$query = "SELECT distinct make FROM car WHERE car_id!=-1";
	if ($result = mysqli_query($con, $query)) {
		if (mysqli_num_rows($result) > 0) {
			while ($row = mysqli_fetch_array($result)) {
				echo '<option value="' . $row["make"] . '">' . $row["make"] . '</option>';
			}
		}
	}
	echo '</select>';

	if(!isset($_POST["make"]) or ($_POST["make"] == "default")) {

		$model = "----Select Make----";
	}
	else if (!isset($_POST["model"]) or ($_POST["model"] == "default")) {
		$model = "----Select Model----";
	}
	else
		$model = $_POST["model"];

	echo '<br>Model: <select name="model" onchange="change()">';
	echo '<option value="' . $model . '">' . $model . '</option>';
	$query = "SELECT distinct model FROM car WHERE make = '" . $make . "'";
	if ($result = mysqli_query($con, $query)) {
		if (mysqli_num_rows($result) > 0) {
			while ($row = mysqli_fetch_array($result)) {
				echo '<option value="' . $row["model"] . '">' . $row["model"] . '</option>';
			}
		}
	}
	echo '</select>';

	if(!isset($_POST['make']) || $_POST["make"] == "----Select Make----") {
		$year = '----Select Make----';
	}
	else if (!isset($_POST["model"]) || ($_POST["model"] == "----Select Model----")) {
		$year = '----Select Model----';
	}
	else if (!isset($_POST["year"]) or ($_POST["year"] == "----Select Year----")) {
		$year = '----Select Year----';
	}
	else {
		$year = $_POST["year"];
	}

	echo '<br>Year: <select name="year" onchange="change()">';
	echo '<option value="' . $year . '">' . $year . '</option>';
	$query = "SELECT distinct year FROM car WHERE make = '" . $make . "' AND model = '" . $model . "'";
	if ($result = mysqli_query($con, $query)) {
		if (mysqli_num_rows($result) > 0) {
			while ($row = mysqli_fetch_array($result)) {
				echo '<option value="' . $row["year"] . '">' . $row["year"] . '</option>';
			}
		}
	}
	echo '</select>'; 

	return;

}




if (!isset($_POST['term'])) {
	// echo "<pre>";
	// print_r($_POST);
	// echo "</pre>";
	?>
	<form method="post" action="">
		<h3>Search Parts By Keyword</h3>
		<br>Keywords: <input type='text' name='term'></input>
		<br>Search by: 	<select name='method'>
			<option value='name'>Name</option>
			<option value='price'>Price</option>
			<option value='category'>Category</option>
			<option value='type'>Type</option>
			<option value='manufacturer'>Manufacturer</option>
			<option value='refurbished'>Refurbished</option>
		</select>
		<br><input type='submit' name='submit' value='search'></input>
	</form>
	<br><br><br>
	<form method='post' action="" id="carForm">
		<h3>Search Parts by Car</h3>
		<br>
		<?php
			carForm($con);
			if (isset($_POST['make']) && isset($_POST['model']) && $_POST['year'] != "----Select Make----") {
				echo "<input type='hidden' name='term' value='set'>";
			}
		?>
		<input type='hidden' name='method' value='car'>
		<br><input type='submit' value='submit'>
	</form>
	
	<?php
} else {
	// $query = "SELECT * FROM item INNER JOIN car ON item.car=car.car_id WHERE car.make='" . $_POST['make'] . "' AND car.model='" . $_POST['model'] . "' AND car.year=" . $_POST['year'];
	// $query = "SELECT * FROM item INNER JOIN car ON item.car=car.car_id WHERE " . $_POST['method'] . " LIKE '%" . $_POST['term'] . "%'";
	if($_POST['method'] == 'car') {
		$query = "SELECT * FROM item INNER JOIN car ON item.car=car.car_id WHERE (car.make='" . $_POST['make'] . "' AND car.model='" . $_POST['model'] . "' AND car.year=" . $_POST['year'] . ") OR (item.car=-1) ORDER BY car.make DESC, car.model, car.year";
	} else {
		$query = "SELECT * FROM item INNER JOIN car ON item.car=car.car_id WHERE " . $_POST['method'] . " LIKE '%" . $_POST['term'] . "%'";
	}
	// echo $query . "<br>";
	if ($result = mysqli_query($con, $query)) {
		if (mysqli_num_rows($result) > 0) {
			$stocked = false;
			while($row = mysqli_fetch_array($result)) {
				$availability = 0;
				if (inStock($row['part_no'], $con)) {
					$stocked = true;
					$query = "SELECT SUM(quantity) FROM stock WHERE itemID=" . $row['part_no'];
					if (! $res = mysqli_query($con, $query))  echo mysqli_error($con) . "<br>";
					$res = mysqli_fetch_array($res);
					$availability = $res[0];
				}

				echo <<<EOT
				<br>Name: $row[name]
				<br>Price: $row[price]
				<br>Availability: $availability
				<br>Category: $row[category]
				<br>Type: $row[type]
				<br>Car: $row[make] $row[model] $row[year]
				<br>Manufacturer: $row[manufacturer]
				<br>Refurbished:
EOT;
				if($row['refurbished']) {
					echo " Yes";
				} else {
					echo " No";
				}
				echo "<br><strong><a href='cart.php?id=" . $row['part_no'] . "&action=add'>Add to Cart</a></strong><br><br>";
				
			}
			if (!$stocked) {
				echo "None of the results for this search are in stock. Please contact us with more information so we can order the part you need.";
			}
		} else {
			echo "There were no results for this query. Please go back and try again.";
		}
	} else {
		echo "There was an error: " . mysqli_error($con) . "<br>";
	}

}

	
require('footer.php');
?>